package cn.tk.service.mr.impl;

import cn.tk.cache.mr.EntityBuf;
import cn.tk.cache.mr.VariableBuf;
import cn.tk.model.pojo.mr.flow.*;
import cn.tk.service.mr.ReplyNodeService;
import cn.tk.utils.EmptyUtil;
import cn.tk.utils.JpaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by denglw on 2021/8/4.<br/>
 * Desc: 话术节点 服务实现层
 */
@Service
public class ReplyNodeServiceImpl implements ReplyNodeService {

    @Autowired
    private JpaMapper jpaMapper;

    @Override
    public ReplyNodeDTO getReplyNode(Long replyNodeId) {
        if (EmptyUtil.nullOrZero(replyNodeId)) {
            return null;
        }
        ReplyNodeDTO dto = this.baseReplyNode(replyNodeId);
        if (dto == null) {
            return null;
        }
        dto.setAction(replyNodeAction(replyNodeId));
        dto.setFilters(replyNodeFilters(replyNodeId));
        dto.setTimeouts(replyNodeTimeouts(replyNodeId));
        return dto;
    }

    @Override
    public ReplyDTO getReply(Long replyId) {
        if (EmptyUtil.nullOrZero(replyId)) {
            return null;
        }
        ReplyDTO dto = this.baseReply(replyId);
        if (dto == null) {
            return null;
        }
        Integer contentType = dto.getContentType();
        // 如果是文本话术
        if (Objects.equals(contentType, 1)) {
            dto.setTextReplyDetails(this.textReplyDetails(replyId));
            dto.setTextReplyOptions(this.textReplyOptions(replyId));
        }
        // 如果是语音话术
        if (Objects.equals(contentType, 2)) {
            dto.setVoiceReplyDetails(this.voiceReplyDetails(replyId));
        }
        return dto;
    }

    @Override
    public ReplyNodeAction replyNodeAction(Long nodeId) {
        if (EmptyUtil.nullOrZero(nodeId)) {
            return null;
        }
        String sql = "select flow_node_id nodeId, action_type actionType, jump_flow_id jumpFlowId, \n" +
                "jump_node_id jumpNodeId from mr_flow_reply_node_action where flow_node_id=:nodeId";
        Map<String, Object> params = new HashMap<>(4);
        params.put("nodeId", nodeId);
        Class<?>[] clazzes = new Class[]{Long.class, Integer.class, Long.class, Long.class};
        return jpaMapper.queryOne(sql, params, ReplyNodeAction.class, clazzes).orElse(null);
    }

    private ReplyNodeDTO baseReplyNode(long nodeId) {
        String sql = "select b.id nodeId, b.default_reply_id defaultReplyId, b.outter_reply_id outterReplyId,b.inner_reply_id innerReplyId, \n" +
                "b.listener, b.if_send_sms smsSend, b.repeat_num repeatNum, b.max_repeat_reply_id maxRepeatReplyId\n" +
                "from mr_bot_flow_node a, mr_flow_reply_node b where a.id=b.id and a.id=:nodeId and a.deleted=0 and a.disabled=0";
        Map<String, Object> params = new HashMap<>(4);
        params.put("nodeId", nodeId);
        Class<?>[] clazzes = new Class[]{Long.class, Long.class, Long.class, Long.class, Integer.class, Boolean.class, Integer.class, Long.class};
        return jpaMapper.queryOne(sql, params, ReplyNodeDTO.class, clazzes).orElse(null);
    }

    private List<ReplyNodeFilter> replyNodeFilters(long nodeId) {
        String sql = "select flow_node_id nodeId, question from mr_flow_reply_node_filter where flow_node_id=:nodeId";
        Map<String, Object> params = new HashMap<>(4);
        params.put("nodeId", nodeId);
        Class<?>[] clazzes = new Class[]{Long.class, String.class};
        return jpaMapper.queryList(sql, params, ReplyNodeFilter.class, clazzes);
    }

    private List<ReplyNodeTimeout> replyNodeTimeouts(long nodeId) {
        String sql = "select flow_node_id nodeId, reply_id replyId, timeout, sort_num sortNum\n" +
                "from mr_flow_reply_node_timeout where flow_node_id=:nodeId";
        Map<String, Object> params = new HashMap<>(4);
        params.put("nodeId", nodeId);
        Class<?>[] clazzes = new Class[]{Long.class, Long.class, Integer.class, Integer.class};
        return jpaMapper.queryList(sql, params, ReplyNodeTimeout.class, clazzes);
    }

    private ReplyDTO baseReply(long replyId) {
        String sql = "select id replyId, content_type contentType, respond_type respondType \n" +
                "from mr_reply where id=:replyId and deleted=0 and disabled=0";
        Map<String, Object> params = new HashMap<>(4);
        params.put("replyId", replyId);
        Class<?>[] clazzes = new Class[]{Long.class, Integer.class, Integer.class};
        return jpaMapper.queryOne(sql, params, ReplyDTO.class, clazzes).orElse(null);
    }

    private List<TextReplyDetail> textReplyDetails(long replyId) {
        String sql = "select id detailId, reply_id replyId, type, content_type contentType, \n" +
                "content, variable_id variableId, entity_id entityId, call_num callNum, sort_num sortNum \n" +
                "from mr_text_reply_detail where reply_id=:replyId";
        Map<String, Object> params = new HashMap<>(4);
        params.put("replyId", replyId);
        Class<?>[] clazzes = new Class[]{Long.class, Long.class, Integer.class, Integer.class, String.class, Long.class, Long.class, Integer.class, Integer.class};
        List<TextReplyDetail> details = jpaMapper.queryList(sql, params, TextReplyDetail.class, clazzes);
        if (EmptyUtil.emptyColl(details)) {
            return new ArrayList<>();
        }
        // 赋值 变量码  实体码
        VariableBuf variableBuf = VariableBuf.getInstance();
        EntityBuf entityBuf = EntityBuf.getInstance();
        for (TextReplyDetail detail : details) {
            detail.setVariableEnName(variableBuf.getEnName(detail.getVariableId()));
            detail.setEntityCode(entityBuf.getCode(detail.getEntityId()));
        }
        return details;
    }

    private List<TextReplyOption> textReplyOptions(long replyId) {
        String sql = "select id optionId, reply_id replyId, content, sort_num sortNum from mr_text_reply_option where reply_id=:replyId";
        Map<String, Object> params = new HashMap<>(4);
        params.put("replyId", replyId);
        Class<?>[] clazzes = new Class[]{Long.class, Long.class, String.class, Integer.class};
        return jpaMapper.queryList(sql, params, TextReplyOption.class, clazzes);
    }

    private List<VoiceReplyDetail> voiceReplyDetails(long replyId) {
        String sql = "select id detailId, reply_id replyId, type, can_disrupted canDisrupted,\n" +
                "content, ref_id variableId, file_path filePath, file_length fileLength, call_num callNum, sort_num sortNum\n" +
                "from mr_voice_reply_detail where reply_id=:replyId";
        Map<String, Object> params = new HashMap<>(4);
        params.put("replyId", replyId);
        Class<?>[] clazzes = new Class[]{Long.class, Long.class, Integer.class,
                Boolean.class, String.class, Long.class, String.class, Integer.class, Integer.class, Integer.class};
        List<VoiceReplyDetail> details = jpaMapper.queryList(sql, params, VoiceReplyDetail.class, clazzes);
        if (EmptyUtil.emptyColl(details)) {
            return new ArrayList<>();
        }
        VariableBuf variableBuf = VariableBuf.getInstance();
        for (VoiceReplyDetail detail : details) {
            Long variableId = detail.getVariableId();
            detail.setVariableEnName(variableBuf.getEnName(variableId));
            detail.setVariableType(variableBuf.getType(variableId));
        }
        return details;
    }

}
